package com.uottawa.library.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class BaseDAO {

	private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
	private static final String DB_URL = "jdbc:mysql://localhost/team7library?allowMultiQueries=true";

	// Database credentials
	private static final String USER = "root";
	private static final String PASS = "";

	protected Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName (JDBC_DRIVER).newInstance ();
			conn = DriverManager.getConnection (DB_URL, USER, PASS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public void cleanUp() {
		String query = "DELETE FROM TRANSACTION_LOG;" +
					   "DELETE FROM LOAN;" +
					   "DELETE FROM LOAN_PERIOD;" +
					   "DELETE FROM USER;" +
					   "DELETE FROM ITEM;" +
					   "DELETE FROM TITLE";
		
		Connection connection = getConnection();
		try {
			Statement stmt = connection.createStatement();
			stmt.execute(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
}

